/* eslint-disable max-lines-per-function */
const Koa = require('koa')
const Router = require('koa-router')

const Mock = require('mockjs')

const app = new Koa()
const router = new Router()

const port = 3000
const hostname = '127.0.0.1'

router.get('/', ctx => {
  ctx.body = 'hello world!'
})

router.get('/api', ctx => {
  ctx.body = Mock.mock({
    status: 200,
    msg: '@paragraph()',
    'data|1-9': [
      {
        'id|+1': 100,
        'string|1': ['hello', 'james', 'zhang'],
        'number|1-10000.3-3000': 300,
        'boolean|1-2': true,
        'object|1-3': {
          '1100': '北京市',
          '1200': '天津市',
          '1300': '河北省',
          '1400': '山西省'
        },
        'array1|+1': ['amd', 'cmd', 'umd', 'common'],
        'array2|1': ['amd', 'cmd', 'umd', 'common'],
        'array3|1-2': [
          {
            'name|+1': ['hello', 'world'],
            'regexp|3': /\d{5,10}-/,
            createTime: "@datetime('yyyy-MM-dd A HH:mm:ss')",
            updateTime: "@now('yyyy-MM-dd A HH:mm:ss')",
            img: "@image('230x320', '#50B347', '#fff', '@last')",
            url: '@url',
            color: '@color()',
            colorHex: '@hex()',
            colorReba: '@rgba()',
            paragraph: '@paragraph(1, 3)',
            word: '@word(3, 5)',
            cparagraph: '@cparagraph()',
            cword: '@cword(3)',
            first: '@first',
            last: '@last',
            name: '@name',
            cfirst: '@cfirst',
            clast: '@clast',
            cname: '@cname',
            email: '@email()',
            ip: '@ip()',
            country: '@country',
            province: '@province',
            city: '@city()',
            uid: '@guid()',
            increment: '@increment(9)',
            userId: '@id()',
            capitalize: '@capitalize(@name)',
            upper: '@upper(@name)',
            lower: '@lower(@name)',
            pick: '@pick(["a", "e", "i", "o", "u"])'
          }
        ]
      }
    ]
  })
})

app.use(async (ctx, next) => {
  // set response header
  ctx.response.set('Access-Control-Allow-Origin', '*')
  ctx.response.set('Access-Control-Allow-Headers', 'X-Requested-Width')
  ctx.response.set('Access-Control-Allow-Headers', 'Content-Type')
  await next()
})

app.use(router.routes()).use(router.allowedMethods())

app.listen(port, hostname)

console.log(`http://${hostname}:${port}`)
